package 题目集.位运算.异或;

/**
 * https://leetcode.cn/problems/number-complement/description/
 */
public class 数字的补数 {

    public int findComplement(int num) {
        int nextPower = (int) (Math.log(num) / Math.log(2)) + 1;
        /*找到和num最高位1相同的全1*/
        int v = (1 << nextPower) - 1;
        /*用全1进行异或，相当于取反*/
        return v ^ num;
    }
}
